package com.lifeng.shop.lifengshop.servies.impl;

import com.lifeng.shop.lifengshop.mapper.ShopcartMapper;
import com.lifeng.shop.lifengshop.mapper.UserMapper;
import com.lifeng.shop.lifengshop.pojo.entry.Product;
import com.lifeng.shop.lifengshop.pojo.entry.Shopcart;
import com.lifeng.shop.lifengshop.servies.ShopcartServies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: LiJiaxun
 * @Date: 2021/8/7 14:05
 */
@Service
public class ShopcartServiesImpl implements ShopcartServies {
    @Autowired
    ShopcartMapper shopcartMapper;

    @Override
    public int deleteByPrimaryKey(Integer uid) {
        return shopcartMapper.deleteByPrimaryKey(uid);
    }

    @Override
    public int insert(Shopcart record) {
        return shopcartMapper.insert(record);
    }

    @Override
    public int insertSelective(Shopcart record) {
        Shopcart shopcart = shopcartMapper.checkProductExist(record);
        if(shopcart == null){
            //  若不存在该商品，则添加该商品到购物车
            return shopcartMapper.insertSelective(record);
        }else {
            //  若已经存在该商品，则只增加数量
            record.setCount(shopcart.getCount() + record.getCount());
            return shopcartMapper.updateCount(record);
        }
    }

    @Override
    public List<Shopcart> selectByPrimaryKey(Integer uid) {
        return shopcartMapper.selectByPrimaryKey(uid);
    }

    @Override
    public int updateByPrimaryKeySelective(Shopcart record) {
        return shopcartMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Shopcart record) {
        return shopcartMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Shopcart> selectUserShopcartByUid(Integer uid) {
        return shopcartMapper.selectUserShopcartByUid(uid);
    }

    @Override
    public int updateCount(Shopcart record) {
        return shopcartMapper.updateCount(record);
    }

    @Override
    public int deleteByGid(Shopcart record) {
        return  shopcartMapper.deleteByGid(record);
    }

    @Override
    public Shopcart checkProductExist(Shopcart record) {
        return shopcartMapper.checkProductExist(record);
    }

}
